
package test_gnt_cakephp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.agilelang.db.generator.GMySql;

import org.agilelang.fgl.parser.Parser;
import org.agilelang.fgl.scanner.Scanner;
import org.agilelang.generator.cakephp.ICakePHP;
import org.agilelang.object.Project;


public class TestMain {
    protected String path_source = "";
    protected String path_xml = "";
    
    public void scanner_parser(String path_source,String in_source,String path_out_xml) {
        Scanner scaner = new Scanner();
        System.out.println("Scaner : " + path_source + in_source+"\n");

        List<File> files = addFiles(null, new File(path_source+ in_source));
        System.out.println(files);
        for (int i = 0; i < files.size(); i++) {
            File lenguaje = files.get(i);
            System.out.println("file " + lenguaje.getAbsolutePath() + "\n");
            if (!scaner.process(lenguaje.getAbsolutePath())) {
                //System.err.println(scaner.getError());
                System.out.println("ERROR : " + scaner.getError() + "\n");
                return;
            }
        }


        System.out.println("Parser..." + "\n");

        Parser parser = new Parser();
        parser.process(scaner.getLines());
        try {
            path_xml = (new File(path_source))+path_out_xml;
            //path_xml = (new File(path_source))+ path_out_xml;
            
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path_xml)));
            System.out.println(path_xml);
            pw.print(parser.getXml());
            pw.close();
            System.out.println("XML Generate : " + path_xml + "\n");
        } catch (IOException e) {
            System.out.println("ERROR : " + e.toString() + "\n");
        }
    }
    
    public List<File> addFiles(List<File> files, File dir) {
        if (files == null) {
            files = new LinkedList<File>();
        }

        if (!dir.isDirectory()) {
            files.add(dir);
            return files;
        }

        for (File file : dir.listFiles()) {
            if (file.getName().endsWith(".agile")) {
                addFiles(files, file);
            }
        }
        return files;
    }

    public static void main(String argv[]) {
        TestMain t = new TestMain();
        
        //String path_source = "D:\\AgileCorp\\agilelang.org\\agile-lang\\version1.3.1\\test.v1.3.1\\src\\test_gnt_cakephp\\testMain\\";
        String path_source = "E:/AgileCorp/agilelang.org/agile-lang2/code/version1.4.1/generator_cakephp/Test";
                            //E:\AgileCorp\agilelang.org\agile-lang2\code\version1.4.1\generator_cakephp\Test\app_agile\article.agile
                            //F:\AgileCorp\agilelang.org\agile-lang\version1.4\AgileLang\Test\test_gnt_cakephp\gestion.agile
        t.scanner_parser(path_source,"/app_agile","/app_agile_out/meta.xml");
        ///t.scanner_parser(path_source,"/example/task_list","/app_agile_out/meta.xml");
        //t.scanner_parser(path_source,"/example/finanza","/app_agile_out/meta.xml");
        String path_app = "E:\\xampp\\htdocs\\agileGestion2";
        //String path_app = "E:\\xampp\\htdocs\\agilecorp.net\\app\\finanza";
        ICakePHP test = new ICakePHP(path_source+"/app_agile_out/meta.xml",path_app);    
        test.setTemplate("bootstrap");
        //test.processFile();
        test.processController();
        test.processModel();
        test.processMenu();
        test.processReport();
        //-----------------------------------------------------
        //String path_file_xml = "D:\\AgileCorp\\agilelang.org\\agile-lang\\version1.3.1\\test.v1.3.1\\src\\test_gnt_cakephp\\meta.xml";
        String path_file_xml = path_source+"/app_agile_out/meta.xml";

        //leer xml
        InputStream is = null;
        Reader reader = null;
        try {
            is = new FileInputStream(path_file_xml );
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //----------------------------------------------------------------------
        //lleno la clase
        Project project = null;
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(Project.class);
            Unmarshaller u = jc.createUnmarshaller();
            project = (Project) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.out.println("mal : " + ex.toString());
        }
        //------------------------------------------------------------------
        
        
        System.out.println(project.application.name);   
        
        GMySql db = new GMySql();
        db.setDB(project.application.source);
        db.setConectionParams(project.application.database,project.application.login,project.application.password);
        db.setModelList(project.models);
        //db.setScaffoldList(project.scaffolds);
        db.writeFileSql(path_source+"/app_agile_out/");  
        //db.createDB();
        System.out.println(db.getFileSql()); 
    }
}
